//
//  CHBaseViewController.swift
//  CHSwift
//
//  Created by cyptt on 2021/5/26.
//
//bian l

import UIKit

public class CHTabBarController: UITabBarController {
    
    private var mTabbarArr:NSArray?
    private var nav:UINavigationController?
    
    private var midClickClouse:(()->())?
    
    
    private var navVC:UINavigationController.Type?

    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        self.tabBar.isTranslucent = false;//tabbar 不透明
        setChildrenControllers()
    }
    
  
    public func setMidClickClouse(closure:@escaping ()->()){
        self.midClickClouse = closure
    }
    
    public init(){
        super.init(nibName: nil, bundle: nil)
    }
    
    public  init(navClass:UINavigationController.Type){
        self.navVC = navClass
        super.init(nibName: nil, bundle: nil)
        
      
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
}

extension CHTabBarController{
    fileprivate func setChildrenControllers(){
        let tabBarArr = CHTabBarStyle.getJsonWithName(name: "chTabbar")
        
        print(tabBarArr)
        self.mTabbarArr = tabBarArr

        var arrVC = [UIViewController]()
        for dict in tabBarArr as Array {
            arrVC.append(childrenController(dict: dict as! [String:AnyObject]))
        }
        viewControllers = arrVC;
    
    }
    
    //使用字典加载控制器
    fileprivate func childrenController(dict: [String : AnyObject]) -> UIViewController{
        
        let tabbarModel:CHTabbarModel = CHTabbarModel.setDicToModel(dic: dict)

        let RootVcName:String? = tabbarModel.RootVcName
        let classIsOC:Bool? = tabbarModel.classIsOC
        let norColorHex:String = tabbarModel.norColor ?? "515151"
        let selColorHex:String = tabbarModel.selColor ?? "0000FF"
        
        let norColor = UIColor.init(hexString: norColorHex)
        let selColor = UIColor.init(hexString: selColorHex)
        
        let fontSize:CGFloat = tabbarModel.fontSize ?? 12
        
        let className:String?
        if classIsOC == true {
            className = RootVcName!
        }else{
            className = Bundle.main.namespace + "." + RootVcName!
        }
        guard  RootVcName != nil,
               let TabbarTitle = tabbarModel.TabbarTitle,
               let NorImgName = tabbarModel.NorImgName,
               let SellectImgName = tabbarModel.SellectImgName,
            let RootVc = NSClassFromString(className!) as? UIViewController.Type
    
        else{
            return UIViewController()
        }
        
        
        //创建控制器视图
        let rootVc = RootVc.init()
        rootVc.title = TabbarTitle
        rootVc.tabBarController?.tabBarItem.title = TabbarTitle

        
        //中间
        let isMid = tabbarModel.isMid
        
        if isMid == true {
            
            let chTabbar:CHTabbar = CHTabbar()
    
            let norImage:UIImage? = UIImage.init(named: NorImgName)
            let selectImage:UIImage? = UIImage.init(named: SellectImgName)
            chTabbar.bulgeButton(normalImage: norImage!, selectImage: selectImage!, count: self.mTabbarArr!.count) {
            
                if self.midClickClouse != nil{
                    self.midClickClouse!()
                }
            }
            chTabbar.setButtonLable(msg: TabbarTitle, color: UIColor.gray,fontSize:12)
            self.setValue(chTabbar, forKey: "tabBar")
            

        }else{
            //设置图片
            rootVc.tabBarItem.image = UIImage(named: NorImgName)?.withRenderingMode(.alwaysOriginal)
            rootVc.tabBarItem.selectedImage = UIImage(named: SellectImgName)?.withRenderingMode(.alwaysOriginal)
            

            
            if #available(iOS 13.0, *) {
              
        
            
            }else{
                
                //未生效
//                rootVc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:norColor], for:.normal)
            }
          

            
            tabBar.unselectedItemTintColor = norColor
            rootVc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: selColor], for: .selected)
         
            rootVc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize)], for: .normal)
        }
     
        
        //实例化导航控制器
    
        if self.navVC != nil{
            let nav = self.navVC!.init(rootViewController: rootVc)
          
            return nav
        }else{
            
            return rootVc
        }
       

     

        
    
    }
}

//获取命名空间
extension Bundle{
    //计算型属性类似于函数，没有参数，有返回值
    var namespace:String{
        return infoDictionary?["CFBundleName"] as? String ?? ""
    }
}
